package com.skyolder.milkbee.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skyolder.milkbee.domain.Card;
import com.skyolder.milkbee.domain.Favorite;
import com.skyolder.milkbee.repository.CardRepository;
import com.skyolder.milkbee.repository.FavoriteRepository;
import com.skyolder.milkbee.service.dto.FavoriteDTO;
import com.skyolder.milkbee.service.mapper.FavoriteMapper;

/**
 * Service Implementation for managing Favorite.
 */
@Service
@Transactional
public class FavoriteService {

	private final Logger log = LoggerFactory.getLogger(FavoriteService.class);

	@Inject
	private FavoriteRepository favoriteRepository;

	@Inject
	private FavoriteMapper favoriteMapper;
	@Inject
	private CardRepository cardRepository;

	/**
	 * Save a favorite.
	 *
	 * @param favoriteDTO
	 *            the entity to save
	 * @return the persisted entity
	 */
	public FavoriteDTO save(FavoriteDTO favoriteDTO) {
		log.debug("Request to save Favorite : {}", favoriteDTO);
		Favorite favorite = favoriteMapper.favoriteDTOToFavorite(favoriteDTO);
		favorite = favoriteRepository.save(favorite);
		FavoriteDTO result = favoriteMapper.favoriteToFavoriteDTO(favorite);
		return result;
	}

	/**
	 * Get all the favorites.
	 * 
	 * @param pageable
	 *            the pagination information
	 * @return the list of entities
	 */
	@Transactional(readOnly = true)
	public Page<FavoriteDTO> findAll(Pageable pageable) {
		log.debug("Request to get all Favorites");
		Page<Favorite> result = favoriteRepository.findAll(pageable);
		return result.map(favorite -> favoriteMapper.favoriteToFavoriteDTO(favorite));
	}

	/**
	 * Get one favorite by id.
	 *
	 * @param id
	 *            the id of the entity
	 * @return the entity
	 */
	@Transactional(readOnly = true)
	public FavoriteDTO findOne(Long id) {
		log.debug("Request to get Favorite : {}", id);
		Favorite favorite = favoriteRepository.findOne(id);
		FavoriteDTO favoriteDTO = favoriteMapper.favoriteToFavoriteDTO(favorite);
		return favoriteDTO;
	}

	/**
	 * Delete the favorite by id.
	 *
	 * @param id
	 *            the id of the entity
	 */
	public void delete(Long id) {
		log.debug("Request to delete Favorite : {}", id);
		favoriteRepository.delete(id);
	}

	public Favorite findByCardIdAndUserId(Long cardId, Long userId) {
		// TODO Auto-generated method stub
		List<Favorite> l = favoriteRepository.findByCardIdAndUserId(cardId, userId);
		;
		if (l == null) {
			return null;
		}
		if (l.size() <= 0) {
			return null;
		}
		return l.get(0);
	}

	public Page<FavoriteDTO> queryByUserId(Long userId, Pageable pageable) {
		Page<Favorite> page0 = favoriteRepository.queryByUserId(userId, pageable);
		List<Long> ids = new ArrayList<Long>();
		for (Favorite f : page0.getContent()) {
			ids.add(f.getCardId());
		}
		List<Card> cardlist = cardRepository.findByUserIdAndIdIn(userId, ids);
		HashMap<Long, Card> cardMap = new HashMap<Long, Card>();
		for (Card c : cardlist) {
			cardMap.put(c.getId(), c);
		}
		final HashMap<Long, FavoriteDTO> favoriteDTOMap = new HashMap<Long, FavoriteDTO>();
		for (Favorite f : page0.getContent()) {
			FavoriteDTO favoriteDTO = new FavoriteDTO();
			favoriteDTO.setId(f.getId());
			favoriteDTO.setCardId(f.getCardId());
			favoriteDTO.setCreatedAt(f.getCreatedAt());
			favoriteDTO.setLeaguerId(f.getLeaguerId());
			favoriteDTO.setUserId(f.getUserId());
			Card card = cardMap.get(f.getCardId());
			if (card != null) {
				favoriteDTO.setPostedAt(card.getCreatedAt());
				favoriteDTO.setTitle(card.getTitle());
				favoriteDTO.setAuthor(card.getAuthor());
			}
			favoriteDTOMap.put(favoriteDTO.getId(), favoriteDTO);
		}
		return page0.map(favorite -> {
			return favoriteDTOMap.get(favorite.getId());
		});
	}

}
